<?php
/**
 * 数据验证类
 * @package core
 * @subpackage class
 * @author
 * @version $Id$
 */
class Validation {
	/**
	 * 定义验证格式
	 */
	static  $_filter = array(
		'number'=>array(
			'desc'=>'数字',
			'regex'=>'/\d+$/',
			'func'=>''
		),
		'english'=>array(
			'desc'=>'英文字母',
			'regex'=>'/^[A-Za-z]+$/',
			'func'=>''
		),
		'email'=>array(
			'desc'=>'电子邮件',
			'regex'=>'/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
			'func'=>''
		),
		'phone'=>array(
			'desc'=>'电话',
			'regex'=>'/^(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/',
			'func'=>''
		),
		'mobile'=>array(
			'desc'=>'手机',
			'regex'=>'^(13|15)\d{9}$/',
			'func'=>''
		),
		'qq'=>array(
			'desc'=>'QQ',
			'regex'=>'/^[1-9]\d{4,12}$/',
			'func'=>''
		),
		'zipcode'=>array(
			'desc'=>'邮编',
			'regex'=>'/^[1-9]\d{5}$/',
			'func'=>''
		),
		'url'=>array(
			'desc'=>'URL地址',
			'regex'=>'/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/',
			'func'=>''
		),
		'ip'=>array(
			'desc'=>'IP地址',
			'regex'=>'',
			'func'=>array(__CLASS__,'checkIP')
		),
		'date'=>array(
			'desc'=>'日期',
			'regex'=>'',
			'func'=>array(__CLASS__,'checkDate')
		),
		'datetime'=>array(
			'desc'=>'时间',
			'regex'=>'',
			'func'=>array(__CLASS__,'checkDateTime')
		),
		'password'=>array(
			'desc'=>'密码',
			'regex'=>'',
			'func'=>array(__CLASS__,'checkPassword')
		),
	);

	/**
	 * 检查数据格式
	 *
	 * @param mixed		$value	待检查的数据
	 * @param string	$type	数据格式类型
	 * @return bool
	 */
	static public function check($value,$type){
		$type = trim(strtolower($type));
		if ( !$type ){
			return false;
		}
		if ( !array_key_exists($type,self::$_filter) ){
			return false;
		}
		$filter_info = self::$_filter[$type];
		if ( $filter_info['regex'] ){
			$r = preg_match($filter_info['regex'],$value);
			if ($r==0||$r==false) {
				return false;
			}
			return true;
		}
		if ( $filter_info['func'] ){
			return call_user_func($filter_info['func'],$value);
		}
	}

	/**
	 * 检查是否是合法IP地址
	 *
	 * @param string	$ip
	 * @return bool
	 */
	static public function checkIP($ip){
		$ip_a = explode(".",$ip);
		if ( count($ip_a)!=4 ){
			return false;
		}
		for ($i=0;$i<4;$i++){
			if ( !is_numeric($ip_a[$i]) ){
				return false;
			}
			if ( $ip_a[$i]<0||$ip_a[$i]>255 ){
				return false;
			}
		}
		return true;
	}

	/**
	 * 检查日期格式是否正确
	 *
	 * @param string	$date	类似：2008-07-10
	 * @return bool
	 */
	static public function checkDate(&$date){
		if ( !preg_match('/^[\d]{4}-[\d]{2}-[\d]{2}$/i',$date) ){
			return false;
		}
		list($y,$m,$d) = explode('-',$date);
		return checkdate($m,$d,$y);
	}

	/**
	 * 检查时间格式是否正确
	 *
	 * @param string	$datetime	时间字符串，如：2008-07-10 18:08:46
	 * @return bool
	 */
	static public function checkDateTime($datetime){
		if ( !preg_match('/^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}$/i',$datetime) ){
			return false;
		}
		$dt = explode(" ",$datetime);
		list($y,$m,$d) = explode("-",$dt[0]);
		if ( !checkdate($m,$d,$y) ){
			return false;
		}
		list($h,$m,$s) = explode(':',$dt[1]);
		if ( $h<0||$h>23 ){
			return false;
		}
		if ( $m<0||$m>59 ){
			return false;
		}
		if ( $s<0||$s>59 ){
			return false;
		}
		return true;
	}

	/**
	 * 验证密码是否合法，密码只能是字符（单字节），不能包含多字节的字符，长度6-12位
	 *
	 * @param string $password
	 * @return boox
	 */
	public static function checkPassword($password){
		$strlen = strlen($password);
		$reallen = mb_strlen($password,'UTF-8');
		if ( $strlen!=$reallen ){
			return false;
		}
		if ( $strlen<6||$strlen>12 ){
			return false;
		}
		return true;
	}


	public function isEnglish($value)
	{
			if (preg_match("/^[A-Za-z]+$/", $value)) {
					return true;
			} else {
					return false;
			}
	}

	public function isChinse($value)
	{
			if (ereg("^[".chr(0xa1)."-".chr(0xff)."]+$", $value)) {
					return true;
			} else {
					return false;
			}
	}

	public function length($value, $max, $min)
	{
			$_iLength = strlen($value);
			if( ($_iLength >= $min) && ($_iLength <= $max) ) {
					return true;
			} else {
					return false;
			}
	}

	public function SBC_DBC($string)
	{
			$Queue = Array(
			'０' => '0', '１' => '1', '２' => '2', '３' => '3', '４' => '4',
			'５' => '5', '６' => '6', '７' => '7', '８' => '8', '９' => '9',
			'Ａ' => 'A', 'Ｂ' => 'B', 'Ｃ' => 'C', 'Ｄ' => 'D', 'Ｅ' => 'E',
			'Ｆ' => 'F', 'Ｇ' => 'G', 'Ｈ' => 'H', 'Ｉ' => 'I', 'Ｊ' => 'J',
			'Ｋ' => 'K', 'Ｌ' => 'L', 'Ｍ' => 'M', 'Ｎ' => 'N', 'Ｏ' => 'O',
			'Ｐ' => 'P', 'Ｑ' => 'Q', 'Ｒ' => 'R', 'Ｓ' => 'S', 'Ｔ' => 'T',
			'Ｕ' => 'U', 'Ｖ' => 'V', 'Ｗ' => 'W', 'Ｘ' => 'X', 'Ｙ' => 'Y',
			'Ｚ' => 'Z', 'ａ' => 'a', 'ｂ' => 'b', 'ｃ' => 'c', 'ｄ' => 'd',
			'ｅ' => 'e', 'ｆ' => 'f', 'ｇ' => 'g', 'ｈ' => 'h', 'ｉ' => 'i',
			'ｊ' => 'j', 'ｋ' => 'k', 'ｌ' => 'l', 'ｍ' => 'm', 'ｎ' => 'n',
			'ｏ' => 'o', 'ｐ' => 'p', 'ｑ' => 'q', 'ｒ' => 'r', 'ｓ' => 's',
			'ｔ' => 't', 'ｕ' => 'u', 'ｖ' => 'v', 'ｗ' => 'w', 'ｘ' => 'x',
			'ｙ' => 'y', 'ｚ' => 'z', '－' => '-'
			);
			return preg_replace("/([\xA3][\xB0-\xB9\xC1-\xDA\xE1-\xFA])/e", "\$Queue[\\1]", $string);
	}


}
//类定义结束
?>